package com.abkj.platform.tail.command;

import com.abkj.platform.util.PublicTool;
import com.abkj.platform.util.SerialPortUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommandFactory
{
  public static byte[] getCommandBuff(String code, byte cmd)
  {
    byte[] buffer = new byte[21];
    buffer[0] = -86;
    buffer[1] = 117;
    buffer[2] = cmd;
    buffer[3] = 0;
    buffer[4] = 14;
    buffer[5] = 0;
    setDataBlock(buffer, code);
    System.out.print("send：");
    PublicTool.printHexString(buffer);
    return buffer;
  }

  public static byte[] getRainFontCommandBuff(String code, byte cmd) {
    byte[] buffer = new byte[21];
    buffer[0] = -86;
    buffer[1] = 117;
    buffer[2] = cmd;
    buffer[3] = 0;
    buffer[4] = 14;
    buffer[5] = 0;
    System.out.println("transducerCode:" + code);
    int index = 6;

    for (int i = 0; i < code.length(); i += 2) {
      buffer[(index++)] = ((byte)Integer.parseInt(code.substring(i, i + 2), 16));
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.set(5, c.get(5) - 1);
    String[] bufferDates = sdf.format(c.getTime()).split("-");
    for (String bufferDate : bufferDates) {
      buffer[(index++)] = ((byte)Integer.parseInt(bufferDate, 16));
    }
    for (String bufferDate : bufferDates) {
      buffer[(index++)] = ((byte)Integer.parseInt(bufferDate, 16));
    }

    buffer[index] = ((byte)getBufferLenghtXOR(buffer));
    System.out.print("send：");
    PublicTool.printHexString(buffer);
    return buffer;
  }

  public static byte[] getCommandReturn(InputStream in) throws IOException, InterruptedException
  {
    if (SerialPortUtil.timeOut(in, 6, 5000)) {
      return null;
    }
    boolean one = false;
    boolean first = false;
    int n = 0;
    while ((n = in.read()) != -1) {
      if (one) {
        if (n == 122) {
          first = true;
          break;
        }
        one = false;
      }
      else if (n == 85) {
        one = true;
      }
    }

    if (first) {
      if (SerialPortUtil.timeOut(in, 4, 5000)) {
        return null;
      }
      byte[] head = new byte[4];
      in.read(head);
      int len = head[2] & 0xFF | (head[1] & 0xFF) << 8;
      byte[] body = new byte[len + 1];
      if (SerialPortUtil.timeOut(in, len + 1, 5000)) {
        return null;
      }
      in.read(body);
      PublicTool.printHexString(body);
      byte[] ret = new byte[len + 7];
      ret[0] = 85;
      ret[1] = 122;
      ret[2] = head[0];
      ret[3] = head[1];
      ret[4] = head[2];
      ret[5] = 0;
      System.arraycopy(body, 0, ret, 6, body.length);
      System.out.print("recv：");
      PublicTool.printHexString(ret);
      return ret;
    }
    return null;
  }

  public static boolean validate(byte[] ret) {
    int r = 0;
    int len = ret.length;
    for (int i = 0; i < ret.length - 1; i++) {
      r ^= ret[i];
    }
    return r == ret[(len - 1)];
  }

  private static void setDataBlock(byte[] buffer, String transducerCode)
  {
    System.out.println("transducerCode:" + transducerCode);
    int index = 6;

    for (int i = 0; i < transducerCode.length(); i += 2) {
      buffer[(index++)] = ((byte)Integer.parseInt(transducerCode.substring(i, i + 2), 16));
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");
    String[] bufferDates = sdf.format(new Date()).split("-");
    for (String bufferDate : bufferDates) {
      buffer[(index++)] = ((byte)Integer.parseInt(bufferDate, 16));
    }

    buffer[index] = ((byte)getBufferLenghtXOR(buffer));
  }

  private static int getBufferLenghtXOR(byte[] buffers)
  {
    int ret = 0;
    byte[] arrayOfByte = buffers; int j = buffers.length; for (int i = 0; i < j; i++) { byte buffer = arrayOfByte[i];
      ret ^= buffer;
    }
    return ret;
  }
}